package com.kaifamiao.servlet;

import com.kaifamiao.jdbc.helper.JdbcHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/customer/remove")
public class CustomerRemoveServlet extends HttpServlet {

    private JdbcHelper helper;

    @Override
    public void init() throws ServletException {
        helper = JdbcHelper.getInstance();
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.println( "<link rel=\"stylesheet\" href=\"/css/customer.css\">");

        String idString = request.getParameter( "id" );
        if( idString != null && !idString.isBlank() ) {
            Integer id = Integer.valueOf(idString);
            helper.execute("DELETE FROM t_customers WHERE id = ? ", id);
            out.println("<p>删除成功</p>");
        }
        out.println( "<p> <a href=\"/customer/list\">返回列表</a></p>" );
    }

    @Override
    public void destroy() {
        helper.release();
    }
}
